package com.nf1000.balancelibrary;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

/**
 * Created by Jason on 2016/3/22.
 */
public class TimeService extends Service {
    private static final String TAG = "TimeService";
    private final Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            Intent selectDeviceIntent = null;
            switch (msg.what) {
                case 1:
                    switch (msg.arg1) {
                        case 0:
                        case 1:
                            selectDeviceIntent = new Intent("ACTION_NOT_FOUND_DEVICE");
                            TimeService.this.sendBroadcast(selectDeviceIntent);
                            return;
                        case 2:
                            selectDeviceIntent = new Intent(BluetoothTools.ACTION_FOUND_DEVICE);
                            TimeService.this.sendBroadcast(selectDeviceIntent);
                            return;
                        case 3:
                            selectDeviceIntent = new Intent(BluetoothTools.ACTION_CONNECT_SUCCESS);
                            TimeService.this.sendBroadcast(selectDeviceIntent);
                            if ("cf".equals(BluetoolUtil.CURRENT_SCALE)) {
                                TimeService.this.getDateFromDevice();
                            }

                            return;
                        default:
                            return;
                    }
                case 2:
                    byte[] readBuf = (byte[]) msg.obj;
                    byte[] read = new byte[msg.arg1];
                    if (read != null) {
                        for (int readMessage = 0; readMessage < read.length; ++readMessage) {
                            read[readMessage] = readBuf[readMessage];
                        }
                    }

                    String var8 = StringUtils.bytes2HexString(read);
                    if (var8.startsWith("cf")) {
                        TimeService.this.sendClose2Device();
                    }

                    selectDeviceIntent = new Intent(BluetoothTools.ACTION_READ_DATA);
                    selectDeviceIntent.putExtra("readMessage", read);
                    TimeService.this.sendBroadcast(selectDeviceIntent);
                    break;
                case 3:
                    byte[] writeBuf = (byte[]) msg.obj;
                    new String(writeBuf);
                    break;
                case 4:
                    selectDeviceIntent = new Intent("ACTION_CONNECT_SUCCESS");
                    TimeService.this.sendBroadcast(selectDeviceIntent);
                    break;
                case 5:
                    selectDeviceIntent = new Intent("ACTION_CONNECT_ERROR");
                    TimeService.this.sendBroadcast(selectDeviceIntent);
            }

        }
    };

    public TimeService() {
    }

    public void onCreate() {
        if (BluetoolUtil.mChatService == null) {
            BluetoolUtil.mChatService = new BluetoothChatService(this, this.mHandler);
        }

        if (BluetoolUtil.mChatService != null && BluetoolUtil.mChatService.getState() == 0) {
            BluetoolUtil.mChatService.start();
        }

        Log.d("TimeService", "开始循环扫描");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent == null){
            return super.onStartCommand(intent, flags, startId);
        }
        sex = intent.getStringExtra("sex");
        height = intent.getIntExtra("height", 170);
        age = intent.getIntExtra("age",28);
        return super.onStartCommand(intent, flags, startId);
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public IBinder onBind(Intent intent) {
        return null;
    }
    private int height = 170;
    private int age = 25;
    private String sex = "01";

    private void getDateFromDevice() {
        StringBuffer sb = new StringBuffer();
        sb.append("01");
        sb.append(sex);
        sb.append("01");
        if (height > 15) {
            sb.append(Integer.toHexString(height));
        } else {
            sb.append("0" + Integer.toHexString(height));
        }

        if (age > 15) {
            sb.append(Integer.toHexString(age));
        } else {
            sb.append("0" + Integer.toHexString(age));
        }

        sb.append("01");
        if (BluetoolUtil.mChatService != null) {
            String bcc = StringUtils.getBCC(StringUtils.hexStringToByteArray(sb.toString()));
            String out = "FE" + sb.toString() + bcc;
            Log.e(TAG, "写入数据：" + out);
            byte[] dataArray = StringUtils.hexStringToByteArray(out);
            BluetoolUtil.mChatService.write(dataArray);
        }

        Log.e(TAG, "=================================================");
    }

    private void sendClose2Device() {
        if (BluetoolUtil.mChatService != null) {
            String out = "fd35000000000035";
            Log.e(TAG, "写入数据：" + out);
            byte[] dataArray = StringUtils.hexStringToByteArray(out);
            BluetoolUtil.mChatService.write(dataArray);
        }

        Log.e(TAG, "=================================================");
    }
}
